/**	@file Fx.cpp
 * 	@brief Implementation of member functions of class Fx. */
#include "Fx.h"
#include "Expr.h"
#include "Number.h"
#include "Sym.h"
namespace sympycpp {

Fx::Fx(Ex * & e, const std::string & s, const allocationPolicy x): Ex(FX), name_(s), e_(e) {
	e = 0;//pointer, that destroys encapsulation of the constructed object, is reseted
}
Fx::Fx(const Expr & e, const std::string & s): Ex(FX), name_(s), e_(e.e_->copy()) {}
Fx::Fx(const Ex * e, const std::string & s): Ex(FX), name_(s), e_(e->copy()) {}
Fx::Fx(const Fx & x): Ex(FX), name_(x.name()) {
	sign(x.sign());
	e_ = x.e_->copy();
}
Fx::~Fx() {
	delete e_;
}
std::string Fx::treeView(const int i) const {
	std::ostringstream stream;
	spaces(i, stream); 
	stream << "> " << name() <<std::endl; 
	stream << e_->treeView(i + 1); 
	return stream.str();
}
std::string Fx::name() const {
	return name_;
}
Ex * Fx::copy() const {
	return new Fx(*this);
}
Ex * Fx::create(Ex * & e, const allocationPolicy) const {
	// This function uses copy constructor because there is not any simplification rules for the general function.
	// Reimplementations of this function in descendents should use wrapper functions that make simplification.
	Ex * f = new Fx(e, name_, STEALING);
	return f;
}
size_t Fx::size() const {
	return 1;
}
std::string Fx::str() const {
	if (sign())
		return unsigned_str();
	else
		return "-" + unsigned_str();
}
std::string Fx::unsigned_str() const {
	if (e_->isNum()) {
		return name_ + "(" + ((Number *)e_)->str2() + ")";
	}
	return name_ + "(" + e_->str() + ")";
}
Ex * Fx::diff(const Sym & x) const {
		Fx * f = (Fx *)this->copy();
		f->name_ = name_ + "'";
		return f;
}
size_t Fx::rsize(const bool all) const {
	return e_->rsize(false) + 1;//false == stop of cutting of multiplicity 
}
size_t Fx::asize() const {
	return e_->asize() + 1;
}

}
